package org.zjz.basket.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.zjz.basket.dto.RegisterChartDTO;
import org.zjz.basket.entity.PlayerEntity;
import org.zjz.basket.entity.TeamEntity;
import org.zjz.basket.global.CommonException;
import org.zjz.basket.mapper.TeamMapper;
import org.zjz.basket.service.PlayerService;
import org.zjz.basket.service.TeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zjz
 * @since 05/08/2022 - 01:21 下午
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, TeamEntity> implements TeamService {

    @Autowired
    PlayerService playerService;

    @Transactional
    @Override
    public void saveUpdate(RegisterChartDTO dto) {
        TeamEntity team = dto.getTeam();
        List<PlayerEntity> playerList = dto.getPlayer();
        boolean teamFlag = this.saveOrUpdate(team);
        boolean playerFlag = playerService.saveOrUpdateBatch(playerList);
        if (!(teamFlag && playerFlag)) {
            throw new CommonException("save or update fail");
        }
    }
}
